package com.example.day01;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.day01.p.Persenter;
import com.example.day01.v.View;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View {

    private Toolbar mToob;
    private RecyclerView mRecy;
    private NavigationView mNa;
    private ArrayList<DataBean.ResultsBean> resultsBeans;
    private Recy_Adapter adapter;
    private LinearLayout mLin;
    private DrawerLayout mDr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Persenter persenter = new Persenter(this);
        persenter.getData();
        initView();
    }

    private void initView() {
        mToob = (Toolbar) findViewById(R.id.toob);
        mRecy = (RecyclerView) findViewById(R.id.recy);
        mNa = (NavigationView) findViewById(R.id.na);
        mLin = (LinearLayout) findViewById(R.id.lin);
        mDr = (DrawerLayout) findViewById(R.id.dr);
        mToob.setTitle("");
        setSupportActionBar(mToob);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDr, mToob, R.string.app_name, R.string.app_name);
        mDr.addDrawerListener(toggle);
        toggle.syncState();

        mRecy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mRecy.setLayoutManager(new LinearLayoutManager(this));
        resultsBeans = new ArrayList<>();
        adapter = new Recy_Adapter(this, resultsBeans);
        mRecy.setAdapter(adapter);


        mDr.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull android.view.View drawerView, float slideOffset) {
                mLin.setX(mNa.getRight());

            }

            @Override
            public void onDrawerOpened(@NonNull android.view.View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull android.view.View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });



    }

    @Override
    public void showData(List<DataBean.ResultsBean> beanList) {
        resultsBeans.addAll(beanList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showToast(String s) {
        Toast.makeText(this, "s", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,0,0,"线性布局");
        menu.add(0,1,0,"网格布局");
        menu.add(0,2,0,"瀑布流");


        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 0:
                mRecy.setLayoutManager(new LinearLayoutManager(this));
                break;
            case 1:
                mRecy.setLayoutManager(new GridLayoutManager(this,2));
                break;
            case 2:
                mRecy.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        }


        return super.onOptionsItemSelected(item);
    }
}
